package com.fourthGroup.dao.impl;

import com.fourthGroup.dao.OrderDao;
import com.fourthGroup.model.Order;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;

@Repository
public class OrderDaoImpl implements OrderDao {
    
    private Map<Long, Order> orderData;
    private AtomicLong idGenerator = new AtomicLong(6); // 从6开始自增
    
    public OrderDaoImpl() {
        orderData = new HashMap<>();
        
        Order order1 = new Order(1L, 1, "songxuanlei", 1, "东北雨姐大粉条", 19.9, 1, 1);
        Order order2 = new Order(2L, 1, "songxuanlei", 2, "花东子粉底", 78.8, 1, 1);
        Order order3 = new Order(3L, 1, "songxuanlei", 3, "澳大利亚鹅羽绒服", 2000.0, 1, 1);
        Order order4 = new Order(4L, 1, "songxuanlei", 4, "南面羽绒服", 2599.0, 1, 1);
        Order order5 = new Order(5L, 1, "songxuanlei", 5, "耐刺篮球鞋", 1599.9, 1, 1);
        
        orderData.put(order1.getOid(), order1);
        orderData.put(order2.getOid(), order2);
        orderData.put(order3.getOid(), order3);
        orderData.put(order4.getOid(), order4);
        orderData.put(order5.getOid(), order5);
    }

    @Override
    public Order queryOrderById(Long oid) {
        return orderData.get(oid);
    }

    @Override
    public Order addOrder(Integer uid, Integer pid) {
        // 模拟创建订单
        long oid = idGenerator.getAndIncrement();
        Order order = new Order();
        order.setOid(oid);
        order.setUid(uid);
        order.setPid(pid);
        order.setNumber(1);
        order.setStatus(0);
        
        // 模拟商品信息
        order.setPname("测试商品-" + pid);
        order.setPprice(99.9);
        
        orderData.put(oid, order);
        return order;
    }

    @Override
    public void addOrder(Order order) {
        if (order.getOid() == null) {
            // 如果没有指定ID，生成新ID
            order.setOid(idGenerator.getAndIncrement());
        }
        orderData.put(order.getOid(), order);
    }

    @Override
    public void updateOrderStatus(Long oid) {
        Order order = orderData.get(oid);
        if (order != null) {
            order.setStatus(1);
            orderData.put(oid, order);
        }
    }
} 